package org.alwayssuper.protocol.commons.transform.realtimev2016;

import org.alwayssuper.protostar.annotation.Field;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * 2016版驱动电机数据格式 (0x02)
 * 参考AICoding报文规则文档
 * @author alwaysSuper
 */
@ToString
@Data
@Accessors(chain = true)
public class DriveMotor {
    // 驱动电机个数:1~253个(由motorInfoList的totalUnit获取)
    private int motorCount;
    
    @Field(totalUnit = 1, desc = "电机信息列表")
    private List<MotorInfo> motorInfoList;

    @ToString
    @Data
    @Accessors(chain = true)
    public static class MotorInfo {
        @Field(length = 1, desc = "驱动电机序号:1~253")
        private int motorNumber;
        
        @Field(length = 1, desc = "驱动电机状态:0x01:耗电;0x02:发电;0x03:关闭;0x04:准备;0xFE:异常;0xFF:无效")
        private int motorStatus;
        
        @Field(length = 1, desc = "驱动电机控制器温度:-40℃~210℃,偏移40℃,0xFF无效")
        private int controllerTemperature;
        
        @Field(length = 2, desc = "驱动电机转速:-20000~45531r/min,偏移20000,0xFFFF无效")
        private int motorSpeed;
        
        @Field(length = 2, desc = "驱动电机转矩:-2000~4553.1N·m,精度0.1,偏移20000,0xFFFF无效")
        private int motorTorque;
        
        @Field(length = 1, desc = "驱动电机温度:-40℃~210℃,偏移40℃,0xFF无效")
        private int motorTemperature;
        
        @Field(length = 2, desc = "电机控制器输入电压:0~6000V,精度0.1V,0xFFFF无效")
        private int inputVoltage;
        
        @Field(length = 2, desc = "电机控制器直流母线电流:-1000A~+1000A,精度0.1A,偏移1000A,0xFFFF无效")
        private int dcBusCurrent;
        
        // =========================== 传输值与真实值转换方法 ===========================
        
        /**
         * 获取控制器温度值 (℃)
         * 传输值范围: 0~250, 偏移量40℃, 表示 -40℃~+210℃, 精度 1℃
         * 0xFE: 异常, 0xFF: 无效
         */
        public double getControllerTemperature() {
            if (controllerTemperature == 0xFE || controllerTemperature == 0xFF) {
                return Double.NaN;
            }
            return controllerTemperature - 40.0;
        }
        
        /**
         * 设置控制器温度值 (℃)
         * 值范围: -40℃~+210℃
         */
        public void setControllerTemperature(double controllerTemperature) {
            if (Double.isNaN(controllerTemperature) || controllerTemperature < -40.0 || controllerTemperature > 210.0) {
                this.controllerTemperature = 0xFF; // 无效
            } else {
                this.controllerTemperature = (int) Math.round(controllerTemperature + 40);
            }
        }
        
        /**
         * 获取电机转速值 (r/min)
         * 传输值范围: 0~65531, 偏移量20000, 表示 -20000r/min~45531r/min, 精度 1r/min
         * 0xFFFE: 异常, 0xFFFF: 无效
         */
        public double getMotorSpeed() {
            if (motorSpeed == 0xFFFE || motorSpeed == 0xFFFF) {
                return Double.NaN;
            }
            return motorSpeed - 20000.0;
        }
        
        /**
         * 设置电机转速值 (r/min)
         * 值范围: -20000r/min~45531r/min
         */
        public void setMotorSpeed(double motorSpeed) {
            if (Double.isNaN(motorSpeed) || motorSpeed < -20000.0 || motorSpeed > 45531.0) {
                this.motorSpeed = 0xFFFF; // 无效
            } else {
                this.motorSpeed = (int) Math.round(motorSpeed + 20000);
            }
        }
        
        /**
         * 获取电机转矩值 (N·m)
         * 传输值范围: 0~65531, 偏移量20000, 表示 -2000N·m~4553.1N·m, 精度 0.1N·m
         * 0xFFFE: 异常, 0xFFFF: 无效
         */
        public double getMotorTorque() {
            if (motorTorque == 0xFFFE || motorTorque == 0xFFFF) {
                return Double.NaN;
            }
            return (motorTorque - 20000) * 0.1;
        }
        
        /**
         * 设置电机转矩值 (N·m)
         * 值范围: -2000N·m~4553.1N·m
         */
        public void setMotorTorque(double motorTorque) {
            if (Double.isNaN(motorTorque) || motorTorque < -2000.0 || motorTorque > 4553.1) {
                this.motorTorque = 0xFFFF; // 无效
            } else {
                this.motorTorque = (int) Math.round(motorTorque * 10 + 20000);
            }
        }
        
        /**
         * 获取电机温度值 (℃)
         * 传输值范围: 0~250, 偏移量40℃, 表示 -40℃~+210℃, 精度 1℃
         * 0xFE: 异常, 0xFF: 无效
         */
        public double getMotorTemperature() {
            if (motorTemperature == 0xFE || motorTemperature == 0xFF) {
                return Double.NaN;
            }
            return motorTemperature - 40.0;
        }
        
        /**
         * 设置电机温度值 (℃)
         * 值范围: -40℃~+210℃
         */
        public void setMotorTemperature(double motorTemperature) {
            if (Double.isNaN(motorTemperature) || motorTemperature < -40.0 || motorTemperature > 210.0) {
                this.motorTemperature = 0xFF; // 无效
            } else {
                this.motorTemperature = (int) Math.round(motorTemperature + 40);
            }
        }
        
        /**
         * 获取输入电压值 (V)
         * 传输值范围: 0~60000, 表示 0V~6000V, 精度 0.1V
         * 0xFFFE: 异常, 0xFFFF: 无效
         */
        public double getInputVoltage() {
            if (inputVoltage == 0xFFFE || inputVoltage == 0xFFFF) {
                return Double.NaN;
            }
            return inputVoltage * 0.1;
        }
        
        /**
         * 设置输入电压值 (V)
         * 值范围: 0V~6000V
         */
        public void setInputVoltage(double inputVoltage) {
            if (Double.isNaN(inputVoltage) || inputVoltage < 0 || inputVoltage > 6000.0) {
                this.inputVoltage = 0xFFFF; // 无效
            } else {
                this.inputVoltage = (int) Math.round(inputVoltage * 10);
            }
        }
        
        /**
         * 获取直流母线电流值 (A)
         * 传输值范围: 0~20000, 偏移量1000A, 表示 -1000A~+1000A, 精度 0.1A
         * 0xFFFE: 异常, 0xFFFF: 无效
         */
        public double getDcBusCurrent() {
            if (dcBusCurrent == 0xFFFE || dcBusCurrent == 0xFFFF) {
                return Double.NaN;
            }
            return (dcBusCurrent - 10000) * 0.1;
        }
        
        /**
         * 设置直流母线电流值 (A)
         * 值范围: -1000A~+1000A
         */
        public void setDcBusCurrent(double dcBusCurrent) {
            if (Double.isNaN(dcBusCurrent) || dcBusCurrent < -1000.0 || dcBusCurrent > 1000.0) {
                this.dcBusCurrent = 0xFFFF; // 无效
            } else {
                this.dcBusCurrent = (int) Math.round(dcBusCurrent * 10 + 10000);
            }
        }
        
        /**
         * 检查控制器温度是否异常
         */
        public boolean isControllerTemperatureAbnormal() {
            return controllerTemperature == 0xFE;
        }
        
        /**
         * 检查控制器温度是否无效
         */
        public boolean isControllerTemperatureInvalid() {
            return controllerTemperature == 0xFF;
        }
        
        /**
         * 检查电机转速是否异常
         */
        public boolean isMotorSpeedAbnormal() {
            return motorSpeed == 0xFFFE;
        }
        
        /**
         * 检查电机转速是否无效
         */
        public boolean isMotorSpeedInvalid() {
            return motorSpeed == 0xFFFF;
        }
        
        /**
         * 检查电机转矩是否异常
         */
        public boolean isMotorTorqueAbnormal() {
            return motorTorque == 0xFFFE;
        }
        
        /**
         * 检查电机转矩是否无效
         */
        public boolean isMotorTorqueInvalid() {
            return motorTorque == 0xFFFF;
        }
    }
    
    // 通过motorInfoList列表长度返回motorCount
    public int getMotorCount() {
        return motorInfoList == null ? 0 : motorInfoList.size();
    }
}